//
//  StoreServiceTest.swift
//  LoopTests
//
//  Created by thomas on 2020/9/23.
//

import XCTest
@testable import Loop

class StoreServiceTest: XCTestCase {

    var task: URLSessionDataTask!
    var date: Date!

    override func setUpWithError() throws {
        task = URLSession.shared.dataTask(with: URL(string: "https://test.com")!)
        date = Date()
    }

    override func tearDownWithError() throws {
    }
}

// MARK: - Request History
extension StoreServiceTest {

    func testInsertRequestSucceed() throws {

        StoreService.shared.insertRequest(task, date: Date())
        let request = StoreService.shared.getRequestHistory(by: Int64(task.taskIdentifier))
        XCTAssertNotNil(request)
    }

    func testGetRequestWithExistId() throws {

        var list = StoreService.shared.getAllRequestHistory()
        if list != nil, list!.isEmpty {
            StoreService.shared.insertRequest(task, date: Date())
            list = StoreService.shared.getAllRequestHistory()
        }

        guard let any = list?.first else {
            fatalError()
        }

        let request = StoreService.shared.getRequestHistory(by: Int64(any.id))
        XCTAssertNotNil(request)
    }

    func testGetRequestWithDoNotExistId() throws {
        let request = StoreService.shared.getRequestHistory(by: 1233231)
        XCTAssertNil(request)
    }

    func testGetAllRequestHistory() throws {
        let requests = StoreService.shared.getAllRequestHistory()
        XCTAssertNotNil(requests)
        XCTAssertGreaterThanOrEqual(requests!.count, 1)
    }
}

// MARK: - RequestResult History
extension StoreServiceTest {

    func testInsertFailureRequestResultSucceed() throws {

        StoreService.shared.insertRequestResult(errorMsg: "Any error", data: nil, date: date)
        let request = StoreService.shared.getRequestResultHistory(by: date)
        XCTAssertNotNil(request)
    }

    func testInsertSucceedRequestResultSucceed() throws {

        let json = """
            {
              "current_user_url": "https://api.github.com/user",
              "current_user_authorizations_html_url": "https://github.com/settings/connections/applications{/client_id}",
              "authorizations_url": "https://api.github.com/authorizations"
            }
        """.data(using: .utf8)

        StoreService.shared.insertRequestResult(errorMsg: nil, data: json, date: date)
        let request = StoreService.shared.getRequestResultHistory(by: date)
        XCTAssertNotNil(request)
        XCTAssertEqual(json, request!.data)
    }

    func testGetRequestResultByDate() throws {

        var list = StoreService.shared.getAllRequestResultHistory()
        if list != nil, list!.isEmpty {
            StoreService.shared.insertRequestResult(errorMsg: "Any error", data: nil, date: date)
            list = StoreService.shared.getAllRequestResultHistory()
        }

        guard let any = list?.first?.time else {
            return
        }

        let request = StoreService.shared.getRequestResultHistory(by: any)
        XCTAssertNotNil(request)
    }

    func testGetLastRequestResult() throws {
        let request = StoreService.shared.getLastRequestResult()
        XCTAssertNotNil(request)
    }

    func testGetAllRequestResultHistory() throws {
        let requests = StoreService.shared.getAllRequestResultHistory()
        XCTAssertNotNil(requests)
        XCTAssertGreaterThanOrEqual(requests!.count, 1)
    }
}
